package com.keqing.tomato.adapter;

import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;

import com.keqing.tomato.R;
import com.keqing.tomato.bean.GroupInfo;
import com.keqing.tomato.bean.HistoryInfo;
import com.keqing.tomato.bean.ItemInfo;

import java.util.ArrayList;
import java.util.List;

public class HistoryItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<ItemInfo> list = new ArrayList<>();

    public HistoryItemAdapter(List<ItemInfo> list) {
        this.list = list;
    }

    @Override
    public int getItemViewType(int position) {
        return list.get(position).getItemType();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewHolder holder = null;
        if(viewType == ItemInfo.type_group){
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_group_layout, parent,false);
            holder = new GroupViewHolder(view);
        }
        if(viewType == ItemInfo.type_child){
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_history_layout, parent, false);
            holder = new ChildViewHolder((view));
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ItemInfo itemInfo = list.get(position);
        int itemType = itemInfo.getItemType();
        if(itemType == ItemInfo.type_group){
            GroupViewHolder groupViewHolder = (GroupViewHolder) holder;
            GroupInfo groupInfo = itemInfo.getGroupInfo();
            groupViewHolder.item_date.setText(groupInfo.getDateStr());
            groupViewHolder.item_duration.setText("专注："+groupInfo.getDurationSum()+"分钟");
            groupViewHolder.item_count.setText("番茄钟：" + groupInfo.getHistoryCount()+"个");
        }
        if(itemType == ItemInfo.type_child){
            ChildViewHolder childHolder = (ChildViewHolder)holder;
            HistoryInfo historyInfo = itemInfo.getHistoryInfo();
            childHolder.item_title.setText(historyInfo.getTitle());
            childHolder.item_range.setText(historyInfo.getStartTime() + " - " + historyInfo.getEndTime());
            childHolder.item_duration.setText(historyInfo.getDuration() + "分钟");
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class ChildViewHolder extends ViewHolder {
        View itemView;
        ImageView item_image;
        TextView item_title;
        TextView item_range;
        TextView item_duration;

        public ChildViewHolder(@NonNull View itemView) {
            super(itemView);
            this.itemView = itemView;
            item_image = itemView.findViewById(R.id.item_image);
            item_title = itemView.findViewById(R.id.item_title);
            item_range = itemView.findViewById(R.id.item_range);
            item_duration = itemView.findViewById(R.id.item_duration);
        }
    }

    static class GroupViewHolder extends ViewHolder {
        View itemView;
        TextView item_date;
        TextView item_duration;
        TextView item_count;

        public GroupViewHolder(@NonNull View itemView) {
            super(itemView);
            this.itemView = itemView;
            item_date = itemView.findViewById(R.id.item_date);
            item_duration = itemView.findViewById(R.id.item_duration);
            item_count = itemView.findViewById(R.id.item_count);
        }
    }

    public static class LineDecoration extends RecyclerView.ItemDecoration {
        @Override
        public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            outRect.set(0, 0, 0, parent.getResources().getDimensionPixelOffset(R.dimen.dividerHeight));
        }
    }
}














